import socket
from DesModule import des_descrypt,des_encryptimport socket
from DesModule import des_descrypt, des_encrypt

# 创建一个TCP socket对象
tcpserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpserver.bind(("172.16.211.247", 9000))
tcpserver.listen(5)  # 监听，最多可以有5个客户端等待连接

while True:  # 无限循环，使服务器能够持续接收客户端的连接
    conn, addr = tcpserver.accept()
    print(conn)  # 打印连接对象

    while True:   # 无限循环，以持续从连接的客户端接收数据
        try:
            data = conn.recv(1024)
            plaintext = des_descrypt(data.decode("utf-8"), "12345678")
            print(plaintext)
            ret_data = input("请回复：")
            ciphertext = des_encrypt(ret_data, "12345678")
            conn.send(ciphertext.encode('utf-8'))
        except Exception:   # 如果在数据传输过程中出现异常，则跳出这次循环，等待下一个连接
            break

    conn.close()
tcpclient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpclient.connect(("172.16.208.90", 2025))
while True:
    while True:
        data = input('>>>').strip()  # 显示提示符 >>> 等待用户输入并去除首尾的空白字符。
        ciphertext = des_encrypt(data, "12345678")
        tcpclient.send(ciphertext.encode('utf-8'))
        ret = tcpclient.recv(1024)
        plaintext = des_descrypt(ret.decode('utf-8'), "12345678")
        print(plaintext)
tcpclient.close()